//app.js
App({
    onLaunch: function() {
        // 登录
        // 获取用户信息
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.login({
                        success: res => {
                            // 发送 res.code 到后台换取 openId, sessionKey, unionId
                            var code = res.code;
                            // console.log(res, 'login')
                            wx.getUserInfo({
                                success: result => {
                                    // 可以将 res 发送给后台解码出 unionId
                                    console.log(result, 'getuserInfo')
                                    result.userInfo.code = code;
                                    result.userInfo.iv = result.iv;
                                    result.userInfo.encryptedData = result.encryptedData;
                                    this.userLogin(result.userInfo);

                                }
                            })
                        }
                    })
                    
                }
            }
        })
       
        
        
        

        //获取主题色
        // wx.request({
        //     url: this.serverPath + '',
        // })
        wx.request({
            url: this.globalData.serverPath + '/settings',
            method: 'post',
            success: ({data}) => {
                // console.log(data)
                var color = data.result.BackColor || '#745BFF';
                this.globalData.primary = color;
                this.usePrimary = (color) => {
                    wx.setNavigationBarColor({
                        frontColor: '#ffffff',
                        backgroundColor: color,  
                    })
                }
                if (this.setPrimary) {
                    this.setPrimary(color)  
                }
            },
            fail: err => {
                var color = '#745BFF';
                this.globalData.primary = color;
                this.usePrimary = (color) => {
                    wx.setNavigationBarColor({
                        frontColor: '#ffffff',
                        backgroundColor: color,
                    })
                }
                if (this.setPrimary) {
                    this.setPrimary(color)
                }
            }
        })
        this.setCartbadge();
    },
    globalData: {
        ossPath: "https://img.umeiyin.com/", // oss 的路径
        serverPath: 'https://api.umeiyin.com/api', // 后台接口
        h5: 'https://h5.umeiyin.com', // 链接的h5
        userInfo: null, // 用户信息
        user: {},
        state: 'msg',
        primary: '', // 主题色
        header: {
            "content-type": 'application/x-www-form-urlencoded'
        },
        code: '', // 
    },
    userInfoReadyCallback: function(data) { //全局一步登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                data.userInfo.code = res.code
                data.userInfo.iv = data.iv;
                data.userInfo.encryptedData = data.encryptedData;
                this.userLogin(data.userInfo, true)
            }
        })
    },
    userLogin: function(data, login) {
        if (login) {
            wx.showLoading({
                title: '登录中',
            })
        }
        wx.request({
            url: this.globalData.serverPath + '/wxminilogin',
            method: 'post',
            header: this.globalData.header,
            data: data,
            success: ({data}) => {
                console.log(data, '后台返回')
                if (data.status == 'success') {
                    this.globalData.userInfo = data.result;
                    this.globalData.header.token = data.result.Token
                    this.getTeamInfo();
                    if (login) {
                        wx.showToast({
                            title: '登录成功',
                            icon: 'none'
                        })
                    }
                    
                }
                if (data.status == 'fail') {
                    wx.showToast({
                        title: '登录失败:' + data.msg,
                        icon: 'none',
                        duration: 0
                    })
                }
            },
            fail: err => {
                console.log(err, '后台错误')
            },
            complete: () => wx.hideLoading()
        })
    },
    checkedUserInfo: function() { // 检测是否授权
        return new Promise(resolve => {
            wx.getSetting({
                success: res => {
                    // 没有授权
                    if (!res.authSetting['scope.userInfo']) {
                        wx.navigateTo({
                            url: '../login/login',
                        })
                        resolve(false) 
                    } else {
                        if (this.globalData.user.team) {
                            resolve(true)
                        } else {
                            var timer = setInterval(() => {
                                if (this.globalData.user.team) {
                                    clearInterval(timer)
                                    resolve(true)
                                }   
                            })
                        }
                    }
                },
                fail: err => {
                    resolve(false)
                    wx.showToast({
                        title: err,
                        icon: 'none'
                    })
                }
            })
        })
        
    },
    getTeamInfo: function() { // 获取去团队信息
        wx.request({
            url: this.globalData.serverPath + '/TeamInfos',
            method: "post",
            header: this.globalData.header,
            success: ({data}) => {
                console.log(data, '团队')
                this.globalData.user.team = data[0].Num; // 保存团队编号
            },
            fail: err => {
                wx.showToast({
                    title: '获取team失败',
                    icon: 'none'
                })
            }
        })
    },
    setCartbadge: function() { // 设置购物车角标
        var cart = wx.getStorageSync('cart');
        try {
            cart = JSON.parse(cart)
        } catch (e) {
            cart = []
        }
        if (cart.length) {
            cart = cart.length + ''
        console.log(cart, 123)
            wx.setTabBarBadge({
                index: 2,
                text: cart,
                success: () => {
                    console.log('ok')
                },
                fail: err => {
                    console.log(err, 456)
                }
            })
        } else {
            wx.removeTabBarBadge({ index: 2 })
        }
    }
})